package cn.ac.iie.di.ban.ycl.wx.nrd.update.commons;

import cn.ac.iie.di.ban.ycl.wx.nrd.update.conf.Configuration;
import cn.ac.iie.di.ban.ycl.wx.nrd.update.conf.ConfigurationException;
import java.util.HashMap;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author zwlin
 */
public class RuntimeEnv {

	public static class STARTUP_SESSIONS {

		public static final String IMSG = "imsg";
		public static final String GMSG = "gmsg";
		public static final String USER = "user";
	}
        
        public static final String PROGRAM_TYPE = "program.type";
        
	public static final String RMQ_RECEIVE_NAMESRV = "rmq.receive.namesrv";
	public static final String RMQ_RECEIVE_TOPIC = "rmq.receive.topic";
	public static final String RMQ_RECEIVE_GROUPNAME_PREFIX = "rmq.receive.groupname.prefix";

	public static final String RMQ_SEND1_NAMESRV = "rmq.send1.namesrv";
	public static final String RMQ_SEND1_TOPIC = "rmq.send1.topic";
	public static final String RMQ_SEND2_NAMESRV = "rmq.send2.namesrv";
	public static final String RMQ_SEND2_TOPIC = "rmq.send2.topic";

	public static final String MONGO1_HOSTS = "mongo1.hosts";
	public static final String MONGO1_USER = "mongo1.user";
	public static final String MONGO1_PASSWD = "mongo1.passwd";
	public static final String MONGO1_DB = "mongo1.db";
	public static final String MONGO1_TABLE = "mongo1.table";

	public static final String MONGO2_HOSTS = "mongo2.hosts";
	public static final String MONGO2_USER = "mongo2.user";
	public static final String MONGO2_PASSWD = "mongo2.passwd";
	public static final String MONGO2_DB = "mongo2.db";
	public static final String MONGO2_TABLE = "mongo2.table";

	private static Configuration conf = null;
	public static HashMap<String, Object> dynamicParams = new HashMap<>();

	public static synchronized void init() throws RuntimeEnvException {
		if (conf == null) {
			conf = Configuration.getConfiguration(ClassLoader.getSystemClassLoader().getResource("mongoloader.properties").getFile());
		}

		getParamAsString(RMQ_RECEIVE_NAMESRV);
		getParamAsString(RMQ_RECEIVE_TOPIC);
		try {
			getParamAsString(RMQ_RECEIVE_GROUPNAME_PREFIX);
		} catch (Exception e) {
			dynamicParams.put(RMQ_RECEIVE_GROUPNAME_PREFIX, "");
		}
		getParamAsString(RMQ_SEND1_NAMESRV);
		getParamAsString(RMQ_SEND1_TOPIC);

		getParamAsString(RMQ_SEND2_NAMESRV);
		getParamAsString(RMQ_SEND2_TOPIC);

		getParamAsString(MONGO1_HOSTS);
		getParamAsString(MONGO1_USER);
		getParamAsString(MONGO1_PASSWD);
		getParamAsString(MONGO1_DB);
		getParamAsString(MONGO1_TABLE);

		getParamAsString(MONGO2_HOSTS);
		getParamAsString(MONGO2_USER);
		getParamAsString(MONGO2_PASSWD);
		getParamAsString(MONGO2_DB);
		getParamAsString(MONGO2_TABLE);

	}

	public static String getParamAsString(String param) throws RuntimeEnvException {
		if (conf == null) {
			throw new RuntimeEnvException("conf not inited. Please call RuntimeEnv.init() first!");
		}
		String val = (String) dynamicParams.get(param);
		if (val == null) {
			val = conf.getString(param, null);
			if (val == null) {
				throw new RuntimeEnvException("param " + param + " not set!");
			}
			dynamicParams.put(param, val);
		}
		return val;
	}

	public static Integer getParamAsInteger(String param) throws RuntimeEnvException {
		Integer val = (Integer) dynamicParams.get(param);
		if (val == null) {
			try {
				val = conf.getInt(param, -1);
			} catch (ConfigurationException ex) {
				throw new RuntimeEnvException("get runtime env error with integer value! param = " + param, ex);
			}
			if (val == -1) {
				throw new RuntimeEnvException("param " + param + " not set!");
			}
			dynamicParams.put(param, val);
		}
		return val;
	}
}
